package com.sinosoft.perfect.system._personalInfo.action;

import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.common.system.BaseAction;
import com.sinosoft.perfect.system._.po.TDisplay;
import com.sinosoft.perfect.system._.po.TTheme;
import com.sinosoft.perfect.system._.po.TUser;
import com.sinosoft.perfect.system._.po.TUserInformation;
import com.sinosoft.perfect.system._.vo.LoginUser;
import com.sinosoft.perfect.system._display.service.IDisplayService;
import com.sinosoft.perfect.system._theme.service.IThemeService;
import com.sinosoft.perfect.system._user.formBean.UserForm;
import com.sinosoft.perfect.system._user.service.IUserService;

@SuppressWarnings("serial")
public class PersonalInfoAction  extends BaseAction {

	private String userMainId;
	
	private IUserService userService;
	
	private UserForm userForm;
	
	private TUserInformation tuserinfo;
	
	private IThemeService themeService;
	
	private IDisplayService displayService;
	
	public IDisplayService getDisplayService() {
		return displayService;
	}
	public void setDisplayService(IDisplayService displayService) {
		this.displayService = displayService;
	}
	public IThemeService getThemeService() {
		return themeService;
	}
	public void setThemeService(IThemeService themeService) {
		this.themeService = themeService;
	}
	public TUserInformation getTuserinfo() {
		return tuserinfo;
	}
	public void setTuserinfo(TUserInformation tuserinfo) {
		this.tuserinfo = tuserinfo;
	}
	public UserForm getUserForm() {
		return userForm;
	}
	public void setUserForm(UserForm userForm) {
		this.userForm = userForm;
	}
	public String getUserMainId() {
		return userMainId;
	}
	public void setUserMainId(String userMainId) {
		this.userMainId = userMainId;
	}
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}


	public String userQueryForEditPersonalUserInfo() {
		try {
			TUser user = new TUser();
			LoginUser loginUser = (LoginUser) session.get("user");
			userMainId = loginUser.getUserMainId();
			user.setUserMainId(userMainId);
			List<TUser> users = userService.selectTUser(user);
			if (users != null && users.size() != 0) {
				request.setAttribute("userEdit", users.get(0));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "editPersonalUserInfo";
	}
	
	
	public String userEditPersonalUserInfo() throws Exception {
		try {
			userService.updateTUserByPK(userForm);
			this.userMainId = userForm.getUserMainId();// 更新后，通过userMainId查询
		} catch (Exception e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return userQueryForEditPersonalUserInfo();
	}
	
	
	public String userInfoQueryForEditPersonalUserInformation() {
		try {
			LoginUser loginUser = (LoginUser) session.get("user");
			userMainId = loginUser.getUserMainId();
			TUserInformation userInfo = new TUserInformation();
			userInfo.setUserMainId(userMainId);
			List<TUserInformation> userInfos = userService.selectTUserInfo(userInfo);
			if (userInfos != null && userInfos.size() != 0)
				request.setAttribute("userInfoEdit", userInfos.get(0));

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "editPersonalUserInformation";
	}

	public String userInfoEditPersonalUserInformation() {
		try {
			userService.updateTUserInfoByPK(tuserinfo);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		this.userMainId = tuserinfo.getUserMainId();// 更新后，通过userMainId查询
		return userInfoQueryForEditPersonalUserInformation();
	}
	
	/**
	 * 查询当前用户主题(fys 11-07-05)
	 */
	public String userThemeQuery() {
		try {
			TUser user = new TUser();
			LoginUser loginUser = (LoginUser) session.get("user");
			userMainId = loginUser.getUserMainId();
			user.setUserMainId(userMainId);
			List<TUser> users = userService.selectTUser(user);
			String themeId = null;
			if (users != null && users.size() != 0) {
				themeId = users.get(0).getThemeId();
				request.setAttribute("themeId", themeId);
			}

			TTheme theme = new TTheme();
			theme.setStatus("1"); // 只查有效的主题
			List<TTheme> themes = themeService.selectByTTheme(theme);
			if (themes != null && themes.size() != 0)
				request.setAttribute("themes", themes);
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "userThemeList";
	}
	
	/**
	 * 查看当前用户模板(fys 11-07-05)
	 */
	public String userDisplayQuery(){
		try {
			TUser user = new TUser();
			LoginUser loginUser = (LoginUser) session.get("user");
			userMainId = loginUser.getUserMainId();
			user.setUserMainId(userMainId);
			List<TUser> users = userService.selectTUser(user);
			String frameModelId = null;
			if (users != null && users.size() != 0) {
				frameModelId = users.get(0).getFrameModelId();
				request.setAttribute("frameModelId", frameModelId);
			}

			TDisplay display = new TDisplay();
			display.setStatus("1"); // 只查有效的模板
			List<TDisplay> displays = displayService.selectByTDisplay(display);
			if (displays != null && displays.size() != 0)
				request.setAttribute("displays", displays);
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		return "userDisplayList";
	}
	
	/**
	 * 删除用户详细信息(fys 11-07-05)
	 */
	public String userInfoEditClearPerson() {
		try {
			TUserInformation userInfo = new TUserInformation();
			userInfo.setId(tuserinfo.getId());
			userInfo.setUserMainId(tuserinfo.getUserMainId());
			userService.updateTUserInfoByPK(userInfo);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		this.userMainId = tuserinfo.getUserMainId();// 更新后，通过userMainId查询
		return userInfoQueryForEditPersonalUserInformation();
	}
}
